package com.qf.mao.dao;

import com.qf.mao.entity.Course;
import com.qf.mao.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 啾咪小魔王
 * @date 2022/4/7 9:44
 * @since JDK 1.8
 */
@SuppressWarnings("all")
public class CourseDao {
    QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
    public List<Course> getCourse() throws SQLException {
        return queryRunner.query("select * from course",new BeanListHandler<>(Course.class));
    }
    public Course getCourseByCourseName(String courseName) throws SQLException {
        return queryRunner.query("select * from course where courseName=?", new BeanHandler<>(Course.class), courseName);
    }

    public Integer addCourse(String courseName) throws SQLException {
        return queryRunner.update("insert into course(courseName) values(?)", courseName);
    }
    public Integer deleteCourseByCid(int cid) throws SQLException {
        return queryRunner.update("delete from course where cid=?", cid);
    }
}
